package com.towery.dbsrcmanager.db.service;

import com.towery.dbsrcmanager.mybatis.base.entity.TDatabaseVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang.StringUtils;
/**
 * Created by User on 2017/1/3.
 */
@Slf4j
public class Db2DatabaseService extends  DatabaseService {

    public String getObjects_sql ;
    public String getColumns_sql ;
    public Db2DatabaseService(TDatabaseVO database) {
        super(database);

        /*取得全部对象SQL*/
        try {
            getObjects_sql =this.getSqlSource("/sql/objects/db2.sql").replaceAll("\\$\\{databaseid\\}",database.getId());
            getColumns_sql =this.getSqlSource("/sql/columns/db2.sql");
        } catch (IOException e) {
            log.error("",e);
        }
    }

    @Override
    public String getTableDDL(String scema, String tablename) throws Exception {
        String columnsSQL= getColumns_sql.replaceAll("\\$\\{scema\\}",scema.trim()).replaceAll("\\$\\{tablename\\}",tablename.trim());


        //注释语句
        StringBuffer ddl_comment =new StringBuffer();

        //创建语句
        StringBuffer ddl_create =new StringBuffer();
        ddl_create.append("CREATE TABLE ");
        ddl_create.append(scema.trim());
        ddl_create.append('.');
        ddl_create.append(tablename.trim());
        ddl_create.append('(');

        List<Map<String,Object>> columns  =this.queryForList(columnsSQL);
        List<String> primaryKeys =new ArrayList<String>();
        if(columns!=null && columns.size()>0){
             int p=0;
             int columnCount =columns.size();
             for(Map<String,Object> column : columns){
                 ddl_create.append(column.get("COLUMNNAME"));
                 ddl_create.append(' ');
                 String type =String.valueOf(column.get("TYPE"));
                 ddl_create.append(type);
                 if("VARCHAR".equals(type) ||  "CHARACTER".equals(type) ||  "CLOB".equals(type) ||  "BLOB".equals(type) ){
                     ddl_create.append('(');
                     ddl_create.append(column.get("LENGTH"));
                     ddl_create.append(')');
                 }
                 else
                 if("DECIMAL".equals(type)){
                     ddl_create.append('(');
                     ddl_create.append(column.get("LENGTH"));
                     ddl_create.append(',');
                     ddl_create.append(column.get("SCALE"));
                     ddl_create.append(')');
                 }

                 if(p!=columnCount-1) {
                     ddl_create.append(",");
                 }
                 String remark  =(String)column.get("REMARK");
                 if(StringUtils.isNotBlank(remark)){
                     ddl_comment.append("COMMENT ON  ");
                     ddl_comment.append(scema.trim());
                     ddl_comment.append('.');
                     ddl_comment.append(tablename.trim());
                     ddl_comment.append('(');
                     ddl_comment.append(column.get("COLUMNNAME"));
                     ddl_comment.append("  IS  ");
                     ddl_comment.append("'");
                     ddl_comment.append(remark.trim());
                     ddl_comment.append("');");
                 }

                 if(column.get("ISPRIMARYKEY")!=null && (Integer)column.get("ISPRIMARYKEY")==1){
                     primaryKeys.add(String.valueOf(column.get("COLUMNNAME")));
                 }
                 p++;
             }
        }

        if(primaryKeys.size()>0) {
            ddl_create.append(", PRIMARY KEY (");
            ddl_create.append(StringUtils.join(primaryKeys, ","));
            ddl_create.append(")");
        }
        ddl_create.append(");");


        StringBuffer ddl =new StringBuffer();
        ddl.append(ddl_create.toString());
        ddl.append(ddl_comment.toString());
        return ddl.toString();
    }

    @Override
    public String getTableDDL(String scema, String tablename ,String tableremark) throws Exception{


        StringBuffer ddl_remark =new StringBuffer();

        if(StringUtils.isNotBlank(tableremark)) {
            ddl_remark.append("COMMENT ON TABLE ");
            ddl_remark.append(scema.trim());
            ddl_remark.append('.');
            ddl_remark.append(tablename.trim());
            ddl_remark.append("  IS  '");
            ddl_remark.append(tableremark.trim());
            ddl_remark.append("';");
        }
        StringBuffer ddl= new StringBuffer();
        ddl.append(this.getTableDDL(scema,tablename));
        ddl.append(ddl_remark);
        return ddl.toString();

    }


    @Override
    public List<Map<String, Object>> getObjects() throws Exception {
        return this.queryForList(getObjects_sql);
    }
}
